package com.hmyd.hmyd_android.view;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;

import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.adapter.ArrayWheelAdapter;
import com.hmyd.hmyd_android.dto.CityModel;
import com.hmyd.hmyd_android.dto.DistrictModel;
import com.hmyd.hmyd_android.dto.ProvinceModel;
import com.hmyd.hmyd_android.utils.XmlParserHandler;
import com.hmyd.hmyd_android.weigets.OnWheelChangedListener;


/**
 * 地址选择
 * 
 * @author wangk
 * 
 */
public class WheelAddressPopupwindow extends PopupWindow implements OnClickListener,
		OnWheelChangedListener {

	private View mainView;
	private View cancel;
	private View ok;

	private Activity context;
	private WheelView mViewProvince;
	private WheelView mViewCity;
	private WheelView mViewDistrict;

	protected String[] mProvinceDatas;
	protected Map<String, String[]> mCitisDatasMap = new HashMap<String, String[]>();
	protected Map<String, String[]> mDistrictDatasMap = new HashMap<String, String[]>();
	protected Map<String, String> mZipcodeDatasMap = new HashMap<String, String>();
	protected String mCurrentProviceName;
	protected String mCurrentCityName;
	protected String mCurrentDistrictName = "";

	protected String mCurrentZipCode = "";

	OnSelectedListener callBck;
	View rootView;

	// 该接口用于返回选择得到的城市数据
	public interface OnSelectedListener {
		public void onSelected(String province,String city,String district);
	}

	protected void initProvinceDatas() {
		List<ProvinceModel> provinceList = null;
		AssetManager asset = context.getAssets();
		try {
			InputStream input = asset.open("province_data.xml");
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser parser = spf.newSAXParser();
			XmlParserHandler handler = new XmlParserHandler();
			parser.parse(input, handler);
			input.close();
			provinceList = handler.getDataList();
			if (provinceList != null && !provinceList.isEmpty()) {
				mCurrentProviceName = provinceList.get(0).getName();
				List<CityModel> cityList = provinceList.get(0).getCityList();
				if (cityList != null && !cityList.isEmpty()) {
					mCurrentCityName = cityList.get(0).getName();
					List<DistrictModel> districtList = cityList.get(0).getDistrictList();
					mCurrentDistrictName = districtList.get(0).getName();
					mCurrentZipCode = districtList.get(0).getZipcode();
				}
			}
			mProvinceDatas = new String[provinceList.size()];
			for (int i = 0; i < provinceList.size(); i++) {
				mProvinceDatas[i] = provinceList.get(i).getName();
				List<CityModel> cityList = provinceList.get(i).getCityList();
				String[] cityNames = new String[cityList.size()];
				for (int j = 0; j < cityList.size(); j++) {
					cityNames[j] = cityList.get(j).getName();
					List<DistrictModel> districtList = cityList.get(j).getDistrictList();
					String[] distrinctNameArray = new String[districtList.size()];
					DistrictModel[] distrinctArray = new DistrictModel[districtList.size()];
					for (int k = 0; k < districtList.size(); k++) {
						DistrictModel districtModel = new DistrictModel(districtList.get(k)
								.getName(), districtList.get(k).getZipcode());
						mZipcodeDatasMap.put(districtList.get(k).getName(), districtList.get(k)
								.getZipcode());
						distrinctArray[k] = districtModel;
						distrinctNameArray[k] = districtModel.getName();
					}
					mDistrictDatasMap.put(cityNames[j], distrinctNameArray);
				}
				mCitisDatasMap.put(provinceList.get(i).getName(), cityNames);
			}
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {

		}
	}

	private String mCountries[];

	public WheelAddressPopupwindow(Activity paramActivity, WheelPopupListener wheelPopupListener,
			int paramInt1, int paramInt2) {
		super(paramActivity);

		context = paramActivity;
		callBck = (OnSelectedListener) context;

		mainView = LayoutInflater.from(paramActivity).inflate(R.layout.wheel_city_popup_window,
				null);

		initViews(mainView);
		initProvinceDatas();
		setUpListener();
		setUpData();

		setContentView(mainView);

		this.setWidth(LayoutParams.MATCH_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);

		// 设置宽度
		/*
		 * setWidth(paramInt1); //设置高度 setHeight(paramInt2);
		 */
		// 设置显示隐藏动画
		setAnimationStyle(R.style.AnimTools);
		// 设置背景透明
		setBackgroundDrawable(new ColorDrawable(0));
	}

	private void setUpData() {

		initProvinceDatas();
		mViewProvince.setViewAdapter(new ArrayWheelAdapter<String>(context, mProvinceDatas));
		mViewProvince.setVisibleItems(5);
		mViewCity.setVisibleItems(5);
		mViewDistrict.setVisibleItems(5);
		updateCities();
		updateAreas();

	}

	private void updateAreas() {

		int pCurrent = mViewCity.getCurrentItem();
		mCurrentCityName = mCitisDatasMap.get(mCurrentProviceName)[pCurrent];
		String[] areas = mDistrictDatasMap.get(mCurrentCityName);

		if (areas == null) {
			areas = new String[] { "" };
		}
		mViewDistrict.setViewAdapter(new ArrayWheelAdapter<String>(context, areas));
		mViewDistrict.setCurrentItem(0);
		mCurrentDistrictName = areas[0];

	}

	private void updateCities() {

		int pCurrent = mViewProvince.getCurrentItem();
		mCurrentProviceName = mProvinceDatas[pCurrent];
		String[] cities = mCitisDatasMap.get(mCurrentProviceName);
		if (cities == null) {
			cities = new String[] { "" };
		}
		mViewCity.setViewAdapter(new ArrayWheelAdapter<String>(context, cities));
		mViewCity.setCurrentItem(0);
		updateAreas();

	}

	private void setUpListener() {
		mViewProvince.addChangingListener(this);
		mViewCity.addChangingListener(this);
		mViewDistrict.addChangingListener(this);

	}

	private void initViews(View root) {
		mViewProvince = (WheelView) root.findViewById(R.id.country);
		mViewProvince.setVisibility(View.VISIBLE);

		mViewCity = (WheelView) root.findViewById(R.id.province);
		mViewCity.setVisibility(View.VISIBLE);

		mViewDistrict = (WheelView) root.findViewById(R.id.city);
		mViewDistrict.setVisibility(View.VISIBLE);

		cancel = mainView.findViewById(R.id.cancel);
		ok = mainView.findViewById(R.id.ok);

		cancel.setOnClickListener(this);
		ok.setOnClickListener(this);

	}

	@Override
	public void onChanged(WheelView wheel, int oldValue, int newValue) {

		// TODO Auto-generated method stub
		if (wheel == mViewProvince) {
			updateCities();
		} else if (wheel == mViewCity) {
			updateAreas();
		} else if (wheel == mViewDistrict) {
			mCurrentDistrictName = mDistrictDatasMap.get(mCurrentCityName)[newValue];
			mCurrentZipCode = mZipcodeDatasMap.get(mCurrentDistrictName);
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.cancel:
			dismiss();
			break;
		case R.id.ok:
			callBck.onSelected(mCurrentProviceName , mCurrentCityName , mCurrentDistrictName);
			dismiss();
			break;
		default:
			break;
		}
	}

}
